$(function () {
    var defaultColunms = BaseTable.initColumn();
    var table = new BSTable(BaseTable.id, "/wx/place/type/list", defaultColunms);
    table.setPaginationType("server");
    BaseTable.table = table.init();
    init();
});

/**
 * 表格类容初始化
 */
var BaseTable = {
    id: "table",	//表格id
    seItem: null,		//选中的条目
    table: null,
};

/**
 * 初始化表格的列
 */
BaseTable.initColumn = function () {
    return [
        {field: 'selectItem',checkbox : true},
        {field: 'id',title : '序号',align: 'center', valign: 'middle',formatter : function(value, row, index) {return index+1;}},
        {title: '类别名称', field: 'name', align: 'center', valign: 'middle'},
        {title: '状态', field: 'status', align: 'center', valign: 'middle', formatter: function(value, row, index){
            return value === 0 ?
                '<span class="label label-danger">禁用</span>' :
                '<span class="label label-success">正常</span>';
        }},
        {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'status', align: 'center', valign: 'middle', formatter: function(value, row, index){
            var text = value == 0?'恢复':'禁用'
            var btnClazz = value == 0?'btn-success':'btn-danger'
            var html = '';
            html += '<div style="display: flex;justify-content: center">'
            html += '<a style="margin: 0 10px" class="btn '+btnClazz+' " href="#" onclick="enabled('+row.id+','+value+')">'+text+'</a>';
            html += '<a style="margin: 0 10px"  class="btn btn-danger " href="#" onclick="remove('+row.id+')"><i class="fa fa-trash-o">删除</i></a>';
            html += '<a style="margin: 0 10px"  class="btn btn-primary " href="#" onclick="getInfo(' + row.id + ')"><i class="fa fa-pencil-square-o">详情</i>';
            html += '</div>'
            return html;
        }}
    ];
};

/**
 * 检查是否选中
 */
BaseTable.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        alert("请先选中表格中的某一记录！");
        return false;
    } else {
        BaseTable.seItem = selected[0];
        return true;
    }
};


/**
 * 查询表格列表
 */
BaseTable.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    BaseTable.table.refresh({query: queryData});
};

function remove(id) {
    var ids = new Array();
    ids.push(id);
    showWarningMsg('确定要删除当前选中的信息吗',function () {
        ajaxSubmit("/wx/place/type/delete", JSON.stringify(ids), function (r) {
            if (r.code === 0) {
                showSuccessMsg("操作成功！",function () {
                    vm.reload()
                });
            } else {
                alert(r.msg);
            }
        })
    })
}

function clearInput(obj) {
    $(obj).prev().val('');
}

function getInfo(id) {
    vm.getInfo(id);
    vm.showList = false;
}

function enabled(id,status) {
    var params = {
        id: id,
        status: status == 0 ? 1 : 0
    };
    showWarningMsg('确定要修改选中的记录？',function () {
        ajaxSubmit('/wx/place/type/update',JSON.stringify(params),function (r) {
            if(r.code == 0){
                showSuccessMsg('操作成功!');
                vm.reload();
            }else{
                alert(r.msg);
            }
        })
    })
}

var vm = new Vue({
    el:'#app',
    data:{
        showList: true,
        data:{
            status:1,
        },
        name:''
    },
    methods: {
        add: function(){
            vm.data = {status:1};
            vm.showList = false;
        },
        del: function () {
        	//获取选中的列
            var ids = getSelections('table', 'id');
            if (ids.length == 0) {
                alert('请选择需要删除的数据！');
                return;
            }
            console.info(ids);
            showWarningMsg('确定要删除选中的记录？',function () {
				ajaxSubmit('/wx/place/type/delete',JSON.stringify(ids),function (r) {
                    if(r.code == 0){
                        showSuccessMsg('操作成功!');
                        vm.reload();
                    }else{
                        alert(r.msg);
                    }
                })
            })
        },
        saveOrUpdate: function () {
            var url = vm.data.id == null ? "wx/place/type/add" : "wx/place/type/update";
            ajaxSubmit(baseURL + url,JSON.stringify(vm.data),function (r) {
                if(r.code == 0){
                    showSuccessMsg('操作成功!');
                    vm.reload();
                }else{
                    alert(r.msg);
                }
            })
        },
        getInfo: function(id){
            $.get(baseURL + "wx/place/type/getInfo/"+id, function(r){
                vm.data = r.data;
                vm.name = vm.data.name;
            });
        },
        reload: function () {
            resetForm();
            BaseTable.search();
            vm.showList = true;
        }
    }
});

function init() {
    $("#Form")
        .bootstrapValidator(
            {
                errorElement: 'span',
                errorClass: 'help-block',
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '该项目不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 20,
                                message: '场地类别长度在1到20之间'
                            },
                            remote: {
                                message: '该场地类别已存在',
                                url: '/wx/place/type/checkTypeName',
                                data: function(validator) {
                                    if (vm.data!=null && vm.name == $('[name="name"]').val()) {
                                        return false;
                                    }
                                    return {
                                        name: vm.data.name,
                                    };
                                },
                            }
                        }
                    },
                    status:{
                        validators: {
                            notEmpty: {
                                message: '该项目不能为空'
                            },
                        }
                    }
                }
            }).on('success.form.bv', function (e) {
        e.preventDefault();
        vm.saveOrUpdate();
        return false;
    });
}